<?php

namespace App\Service\Admin;

use App\Model\Admin\MenuModel;
use App\Service\BaseService;

/**
 * 系统菜单管理-服务类
 * @author LZH
 * @since: 2021/08/12
 * Class MenuService
 */
class MenuService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new MenuModel();
        parent::__construct();
    }

    /**
     *
     * @return array
     * @author LZH
     */
    public function getList()
    {
        // 查询条件
        $map = [];

        // 菜单标题
        $title = $this->getRequestParam('title');
        if ($title) {
            $map[] = ['title' => ["%{$title}%",'like']];
        }
        return $this->model->getList($map, ["sort" => "asc"]);
    }

    /**
     * 获取指定用户的权限
     * @param $userId
     * @return array|mixed
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     * @author LZH
     */
    public function getPermissionList($userId)
    {
        $list = [];
        if ($userId == 1) { // TODO 这里需要替换为角色名称
            // 管理员拥有全部权限
            $this->log_info('超级管理员获取所有权限');
            $list = $this->model->getChilds(0);
        } else {
            // 其它角色
            $list = $this->model->getPermissionMenu($userId, 0);
        }
        return $list;
    }

}
